import request from '@/config/axios'
import type { User } from './types'

export const getUsersListApi = (params: any): Promise<IResponse> => {
  return request.get({ url: '/users', params })
}

export const createUsersApi = (data: Partial<User>): Promise<IResponse> => {
  return request.post({ url: '/users', data })
}

export const createHotelUsersApi = (data: Partial<User>, hotel_id: string): Promise<IResponse> => {
  return request.post({ url: `/users/hotel/${hotel_id}`, data })
}

export const updateUsersApi = (id: string, data: Partial<User>): Promise<IResponse> => {
  return request.put({ url: `/users/id/${id}`, data })
}

export const getUsersDetailApi = (id: string): Promise<IResponse<User>> => {
  return request.get({ url: `/users/id/${id}` })
}

export const delUsersListApi = (ids: string[] | number[]): Promise<IResponse> => {
  return request.delete({ url: `/users/id/${ids[0]}` })
}

export const getUserInfoApi = (): Promise<IResponse> => {
  return request.get({ url: `/user/info` })
}

export const unlockUserApi = (data): Promise<IResponse> => {
  return request.post({ url: `/user/unlock`, data })
}

export const updatePasswordApi = (data): Promise<IResponse> => {
  return request.post({ url: `/users/update-password`, data })
}

export const resetPasswordApi = (data): Promise<IResponse> => {
  return request.post({ url: `/user/reset-password`, data })
}

export const getUserHotelPermissionsApi = (
  user_id?: string,
  hotel_id?: string
): Promise<IResponse> => {
  return request.get({ url: `/users/permissions/user/${user_id}/hotel/${hotel_id}` })
}

export const grantUserHotelApi = (data): Promise<IResponse> => {
  return request.post({ url: `/users/grant/hotel`, data })
}

export const grantUserOutletApi = (data): Promise<IResponse> => {
  return request.post({ url: `/users/grant/outlet`, data })
}

export const getGrantedUserOutletApi = (user_id, hotel_id): Promise<IResponse> => {
  return request.get({ url: `/users/grant/outlet/user/${user_id}/hotel/${hotel_id}` })
}

export const grantUserHotelPermissionsApi = (user_id, hotel_id, data): Promise<IResponse> => {
  return request.post({
    url: `/users/grant/permissions/user/${user_id}/hotel/${hotel_id}`,
    data
  })
}

export const revokeUserHotelApi = (data): Promise<IResponse> => {
  return request.post({ url: `/users/revoke/hotel`, data })
}

export const getUserGrantedHotelsApi = (id?: string): Promise<IResponse> => {
  return request.get({ url: `/users/id/${id}/grant/hotels` })
}

// 更新已登录主体(登录后切换主体)
export const updateLoginHotelApi = (hotel_id: string): Promise<IResponse> => {
  return request.post({ url: `/users/info/session-hotel/${hotel_id}` })
}

export const getUserByLoginNameApi = (login_name: string): Promise<IResponse> => {
  return request.get({ url: `/users/login_name/${login_name}` })
}
